<?php
/*
Author : Alex
Time   : 2016-6-18
业务范围： 用于Get牛的牛人认证类而编写的Controller
           1.识别当前牛人认证的状态
           2.分发对应的认证页面
           3.存储当前输入的数据并过滤，反馈
*/
class BeNew extends CI_Controller
{
	protected static $apikey = "a7f7ad617f2f54a6acd5d51828abcfdc";
	public function __construct()
	{
		parent::__construct();
		
		$this->load->library('session');
		$this->load->model('newcenter_model');
		$this->load->model('getnew_model');
		$this->load->model('benew_model');
		$this->load->model('picture_model');
		if(isset($_SESSION['wechat']))
		{
			$_SESSION['id'] = $this->getnew_model->getUserId();
			$_SESSION['phone'] = $this->newcenter_model->getPhone();
		}
		else
		{
			exit('非法访问，请从微信进入');
		}
	}
	
	public function getAuthState()
	{
		echo json_encode($this->newcenter_model->getAuthState());//返回认证状态
	}
	
	public function saveState0()
	{
		//保存牛人认证状态，并更新状态为1
		$data = $this->input->post();
		$result = $this->save0($data);
		echo json_encode($result);
	}
	
	public function setTopicId()
	{
		//根据提交的话题ID，设置SESSSION，并且返回值
		$id = $this->input->post('id');
		if($this->benew_model->getIfTopic($id))
		{
			$_SESSION['topic_id'] = $id;
			echo 1;
		}
		else
		{
			echo 2;
		}
	}
	
	public function getTopicDetail()
	{
		//获取当前topic的详细信息
		$id = $_SESSION['topic_id'];
		//$id = 1;
		if(!empty($id))
		{
			$data = ($this->benew_model->getTopicById($id));
			$data['pic'] = $this->picture_model->getPictureById($data['pic_id']);
		}
		else
		{
			$data = (array('flag' => 'failed'));

		}
		//$this->load->view('new_benew/page_topic_detail.php',$data);
		echo json_encode($data);
	}

	public function setTopicDetail()
	{
		$data = $this->input->post();
		$data['topic_id'] = $_SESSION['topic_id'];
		$this->benew_model->updateTopic($data);
		echo json_encode(array('flag'=>'200'));
	}
	
	public function AddTopic()
	{
		$data = $this->input->post();
		$this->benew_model->insertTopic($data);
		echo json_encode($data);
	}
	
	public function index()
	{
		//根据当前审核状态，进入相应的页面
		//$id = $_SESSION['id'];
		$data = array();
		$state = $this->newcenter_model->getAuthState();
		//$state = $this->new_newperson->getState();
		switch($state)
		{
			case 1:
			$this->load->view('new_benew/page1',$data);
			break;
			case 2:
			$this->load->view('new_benew/page2',$data);
			break;
			case 3:
			$this->load->view('new_benew/page3',$data);
			break;
			case 4:
			$this->load->view('new_benew/page4',$data);
			break;
			default:
			$this->load->view('new_benew/page0',$data);
			break;
		}
	}

	public function save()
	{
		//认证状态的入口，分发不同的认证状态
		$data = $this->input->post();
		//$state = $this->new_newperson->getState();
		//echo 'f';exit;
		$state = 0;
		switch($state)
		{
			case 1:
			$result=$this->save1($data);break;
			case 2:
			$result=$this->save2($data);break;
			case 3:
			$result=$this->save3($data);break;
			case 4:
			$result=$this->save4($data);break;
			default:
			$result=$this->save0($data);
			break;
		}
		echo json_encode($result);
	}
	protected function save0($data)
	{
		//处理处于认证状态0的信息，若成功则更新状态为1，同时返回成功信息
		$result = array();
		$count = 0;
		foreach($data as  $r=>$v)
		{
			if(empty($v) && $r!='phone' && $r!='code')
			{
				$result[]=$r;
				$count++;
			}
		}//验空
		$phone_flag = ($data['phone'] == $_SESSION['phone'] && isset($data['phone']) && $_SESSION['phone'] != 'null') ? true:false;
		//检查手机号，确保提交上来的和SESSINO中的一致；
		if($count !=0 )
		{
		   return $result;
		}
		else
		{
			if((!isset($_SESSION['code']) || $_SESSION['code'] != $data['code']) && !$phone_flag )
			{
				return array("flag" => "code_wrong");
			}
			if(!isset($_SESSION['phone']) && !$phone_flag)
			{
				return array("flag"=>'phone_unmark');
			}
			$this->newcenter_model->updateDetail($data);
			$this->newcenter_model->updateAuthState(0);
			return array('flag'=>'success','state'=>0);
		}
		
	}

	protected function save1($data)
	{
		$data = array();
		
	}

    //进入认证状态0的入口在NewCenter/getState

	public function getDetail1()
	{
		//进入认证状态1 
		echo json_encode($this->benew_model->getTopics());
	}

	

	public function varifyPhone()
	{
		if(!$this->checkNumFormat($this->input->post('phone')))
		{
			echo json_encode(array('code' => 'error'));
	     	exit;
		}
		$code= $this->randNum();
		$mobile = $this->input->post('phone');
		$text="【GET牛】您的验证码是".$code;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($ch, CURLOPT_TIMEOUT, 10);


        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $data=array('text'=>$text,'apikey'=>BeNew::$apikey,'mobile'=>$mobile);
        $json_data = $this->send($ch,$data);
        $array = json_decode($json_data,true);
        if($array['code'] == 0)
        {
        	$_SESSION['phone'] = $mobile;
        	$_SESSION['code'] = $code;
            //var_dump($array);
        }
        else
        {
        	//echo json_encode(array(''))
        }
           echo $json_data;
        curl_close($ch);
	}

    protected function send($ch,$data)
    {
        curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($ch);
    }
    protected function randNum()
    {
    	//生成四位数的随机数字
    	$num = '';
    	for($i=0;$i<4;$i++)
    	{
    		$num.=rand(0,9);
    	}
    	return $num;
    }
	protected function checkNumFormat($phone)
	{
		if(preg_match('/^1[3|4|5|8|9][0-9]\d{4,8}$/',$phone) == true)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
} 